//
//  NPHomeHeaderBar.m
//  newProject
//
//  Created by Mac mini on 2017/11/21.
//  Copyright © 2017年 zt. All rights reserved.
//

#import "NPHomeHeaderBar.h"

@implementation NPHomeHeaderBar

//-(void)setButtonTitleArray:(NSArray *)buttonTitleArray{
//    for (int i = 0; i<buttonTitleArray.count; i++) {
//        UIButton *button = _buttonArray[i];
//        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
//    }
//}

-(void)markBoundWithIndex:(NSInteger)index andImageName:(NSString *)imageName{
    UIButton *button = _buttonArray[index];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(button.frame.size.width-25, 0, 20, 20)];
    imageV.image = [UIImage imageNamed:imageName];
    [button addSubview:imageV];
}

-(instancetype)initWithFrame:(CGRect)frame andTitleArray:(NSArray *)titleArr{
    if (self = [super initWithFrame:frame]) {
        NSArray *titleArray = titleArr;
        
         CGFloat buttonW = frame.size.width/titleArr.count;
        
        [self setContentSize:CGSizeMake(buttonW*titleArray.count, 0)];
        self.showsHorizontalScrollIndicator = NO;
        for (int i = 0; i<titleArray.count; i++) {
            
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*buttonW, 0, buttonW, frame.size.height-4)];
            [button setBackgroundColor:[UIColor whiteColor]];
            if (i == 0) {
                button.selected = YES;
                _selectButton = button;
                _bottomLab = [[UILabel alloc]initWithFrame:CGRectMake(15, frame.size.height-2, buttonW-30, 2)];
                
                _bottomLab.backgroundColor = PUBLIC_BLUE;
                [self addSubview:_bottomLab];
                [button setTitle:titleArray[i] forState:UIControlStateNormal];
            }
            

            [button setTitle:titleArray[i] forState:UIControlStateNormal];
            [button setTitleColor:PUBLIC_DARK forState:UIControlStateNormal];
            [button setTitleColor:PUBLIC_BLUE forState:UIControlStateSelected];
            
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            [button setTag:star_commodity+i];
            [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            
           
            
            [self addSubview:button];
            
            if(!_buttonArray){
                _buttonArray = [NSMutableArray arrayWithCapacity:0];
            }
            [_buttonArray addObject:button];
        }
        
//        UIButton *button = _buttonArray[0];
//                        UIImageView *hotImageV = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(button.frame)-5, CGRectGetMinY(button.frame), 20, 20)];
//                        hotImageV.image = [UIImage imageNamed:@"fire"];
//                        [self addSubview:hotImageV];
       
    }
    return self;
}

-(void)click:(UIButton *)button{
    _bottomLab.frame = CGRectMake(button.frame.origin.x+15, _bottomLab.frame.origin.y, _bottomLab.frame.size.width, _bottomLab.frame.size.height);
    if (_selectButton) {
        _selectButton.selected = NO;
    }
    
    button.selected = YES;
    _selectButton = button;
    
    if (self.barDelegate && [self.barDelegate respondsToSelector:@selector(homeHeaderBarClickWithIndex:)]) {
        [self.barDelegate homeHeaderBarClickWithIndex:button.tag];
    }
}

-(void)setScrollLabWithX:(CGFloat)x{
    [UIView animateWithDuration:.1 animations:^{
        _bottomLab.frame = CGRectMake(self.frame.size.width * x+15, _bottomLab.frame.origin.y, _bottomLab.frame.size.width, _bottomLab.frame.size.height);
    }];
    
   
}

-(void)setScrollButtonWithX:(NSInteger)page{
    self.selectButton.selected = NO;
    self.buttonArray[page].selected = YES;
    self.selectButton = self.buttonArray[page];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
